﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Linq;

namespace Lisanslama.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            LisansKontrol();
            DovizKurlari();
            return View();
        }

        public ActionResult Uyari()
        {
            return View();
        }

        private void LisansKontrol()
        {
            string siteAdresi = Request.Url.AbsoluteUri.ToString().TrimEnd('/');
            string LisansAdres = "";
            string Versiyon = "";
            string LisansKodu = "";
            string LisansBaslangicTarihi = "";
            string BitisTarihi = "";
            string BaslangicTarihi = DateTime.Now.ToString("dd.MM.yyyy");
            string[] AdresParcala, VersiyonParcala, BaslangicTarihiParcala, BitisTarihiParcala, LisansNoParcala;


            string Url = "http://www.siteadresi.com/Lisans.xml";

            XElement xEmp = XElement.Load(Url);
            var empNames = from name in xEmp.Elements("Site")
                           select name;

            foreach (XElement fName in empNames)
            {
                LisansAdres += fName.Element("SiteAdi").Value.ToString().TrimEnd('/') + ",";
                Versiyon += fName.Element("Versiyon").Value + ",";
                LisansKodu += fName.Element("LisansNo").Value + ",";
                LisansBaslangicTarihi += fName.Element("BaslangicTarihi").Value + ",";
                BitisTarihi += fName.Element("BitisTarihi").Value + ",";
            }

            LisansAdres = LisansAdres.TrimEnd(',');
            Versiyon = Versiyon.TrimEnd(',');
            LisansBaslangicTarihi = LisansBaslangicTarihi.TrimEnd(',');
            BitisTarihi = BitisTarihi.TrimEnd(',');
            LisansKodu = LisansKodu.TrimEnd(',');

            AdresParcala = LisansAdres.Split(',');
            VersiyonParcala = Versiyon.Split(',');
            BaslangicTarihiParcala = LisansBaslangicTarihi.Split(',');
            BitisTarihiParcala = BitisTarihi.Split(',');
            LisansNoParcala = LisansKodu.Split(',');

            for (int i = 0; i < AdresParcala.Length; i++)
            {
                if (siteAdresi == AdresParcala[i].ToString())
                {
                    int BTarih = DateTimeToInt(Convert.ToDateTime(BaslangicTarihi));
                    int BitTarih = DateTimeToInt(Convert.ToDateTime(BitisTarihiParcala[i].ToString()));

                    if (BitTarih >= BTarih)
                    {
                        ViewBag.SiteAdi = AdresParcala[i].ToString();
                        ViewBag.Versiyon = VersiyonParcala[i].ToString();
                        ViewBag.BaslangicTarihi = BaslangicTarihiParcala[i].ToString();
                        ViewBag.BitisTarihi = BitisTarihiParcala[i].ToString();
                        ViewBag.LisansKodu = LisansNoParcala[i].ToString();
                    }
                    else
                    {
                        Response.Redirect("http://www.siteadresi.com/Uyari.html");
                    }
                }
                else
                {
                    Response.Redirect("http://www.siteadresi.com/Uyari.html");
                }
            }
        }

        private void DovizKurlari()
        {
            XmlTextReader oku = new XmlTextReader("http://www.tcmb.gov.tr/kurlar/today.xml");
            XmlDocument dok = new XmlDocument();
            dok.Load(oku);
            XmlNode xdollar = dok.SelectSingleNode("/Tarih_Date/Currency[CurrencyName='US DOLLAR']");
            XmlNode xeuro = dok.SelectSingleNode("/Tarih_Date/Currency[CurrencyName='EURO']");
            XmlNode xsterling = dok.SelectSingleNode("/Tarih_Date/Currency[CurrencyName='POUND STERLING']");

            string DolarAlis = xdollar.ChildNodes[3].InnerText;
            string DolarSatis = xdollar.ChildNodes[4].InnerText;
            string EuroAlis = xeuro.ChildNodes[3].InnerText;
            string EuroSatis = xeuro.ChildNodes[4].InnerText;
            string PaundAlis = xsterling.ChildNodes[3].InnerText;
            string PaundSatis = xsterling.ChildNodes[4].InnerText;

            ViewBag.DolarAlis = DolarAlis + " TL";
            ViewBag.DolarSatis = DolarSatis + " TL";
            ViewBag.EuroAlis = EuroAlis + " TL";
            ViewBag.EuroSatis = EuroSatis + " TL";
            ViewBag.PaundAlis = PaundAlis + " TL";
            ViewBag.PaundSatis = PaundSatis + " TL";
        }

        public static int DateTimeToInt(DateTime theDate)
        {
            return (int)(theDate.Date - new DateTime(1900, 1, 1)).TotalDays + 2;
        }
    }
}
